package com.shop.cereshop.app.service.coupon.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.cereshop.app.dao.activity.CereBuyerCouponDAO;
import com.shop.cereshop.app.dao.activity.CerePlatformActivityDAO;
import com.shop.cereshop.app.dao.buyer.CereBuyerShopCouponDAO;
import com.shop.cereshop.app.dao.coupon.CereCommonCouponDAO;
import com.shop.cereshop.app.page.coupon.CommonCoupon;
import com.shop.cereshop.app.page.discount.PlatformDiscountProduct;
import com.shop.cereshop.app.service.coupon.CereCommonCouponService;
import com.shop.cereshop.commons.constant.IntegerEnum;
import com.shop.cereshop.commons.domain.common.Page;
import com.shop.cereshop.commons.domain.common.PageParam;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CereCommonCouponServiceImpl implements CereCommonCouponService {

    @Autowired
    private CereCommonCouponDAO cereCommonCouponDAO;

    @Autowired
    private CereBuyerCouponDAO cereBuyerCouponDAO;

    @Autowired
    private CereBuyerShopCouponDAO cereBuyerShopCouponDAO;

    @Override
    public Page<CommonCoupon> getCouponCenterList(PageParam param, Long buyerUserId) {
        PageHelper.startPage(param.getPage(), param.getPageSize());
        List<CommonCoupon> list = cereCommonCouponDAO.selectCouponList();
        if (CollectionUtils.isNotEmpty(list) && buyerUserId != null) {
            for (CommonCoupon coupon:list) {
                int userTakeCount = 0;
                if (coupon.getSource() == 1) {
                    userTakeCount = cereBuyerCouponDAO.findCount(buyerUserId, coupon.getCouponId());
                } else {
                    userTakeCount = cereBuyerShopCouponDAO.findCount(buyerUserId, coupon.getCouponId());
                }
                coupon.setUserTakeCount(userTakeCount);
                if (userTakeCount > 0) {
                    coupon.setState(0);
                }
            }
        }
        PageInfo<CommonCoupon> pageInfo = new PageInfo<>(list);
        return new Page<>(pageInfo.getList(), pageInfo.getTotal());
    }
}
